<?php

namespace App\Http\Controllers\System;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Session,DB;
use App\Http\ClassBox\globals;

class DemanderController extends Controller
{
    public function index()//页面展示
    {
        $clique=DB::table('business')->select('id','Name')->where('Type',2)->where('Del',1)->get();
        if($clique!=NULL)
        {
           $firstCom=DB::table('business')->select('id','Name')->where('CliqueId',DB::table('business')->where('Type',2)->where('Del',1)->first()->id)->where('Type',4)->where('Del',1)->get();
        }
        else
        {
            $firstCom=NULL;
        }
        return view('system.demander',[
            'clique'=>$clique,//集团渲染
            'firstCom'=>$firstCom,//第一个集团所属公司渲染
            'pact'=>DB::table('pact')->select('id','Name')->where('Type',0)->where('Del',1)->get(),//合同渲染
            'getTree_list'=>$this->getTree_list()//组织列表渲染
        ]);
    }

    public function ajax_select(Request $request)//select请求
    {
        switch($request->input('Type'))
        {
        case 2:
        $data['data']=DB::table('business')->select('id','Name','Type')->where('CliqueId',$request->input('pid'))->where('Type',4)->where('Del',1)->get();
        return json_encode($data);
        break;
        case 4:
        $data['data']=DB::table('business')->select('id','Name','Type')->where('ComId',$request->input('pid'))->where('Type',6)->where('Del',1)->get();
        return json_encode($data);
        break;
        case 6:
        $data['data']=DB::table('business')->select('id','Name','Type')->where('DepId',$request->input('pid'))->where('Del',8)->get();
        return json_encode($data);
        break;
        default:
        return 0;
        }
    }

    //发包单位组织架构添加和修改功能
    public function create(Request $request)
    {
        if($request->has('listId'))//条目修改功能
        {
            switch($request->input('Type'))
            {
                case 2:
                $insert=array(
                    'Name'=>trim($request->input('Name')),
                    'Type'=>2,
                    'CliqueId'=>0,
                    'UseId'=>session('user')[0]->id,
                    'PId'=>0
                );
                if($request->has('PactId'))
                {
                    $insert['PactId']=$request->input('PactId');
                }
                else
                {
                    $insert['PactId']=0;
                }
                DB::table('business')->where('id',$request->input('listId'))->update($insert);
                return redirect('system/demander/index')->with('callback', '集团修改成功');
                break;
                case 4:
                $insert=array(
                    'Name'=>trim($request->input('Name')),
                    'Type'=>4,
                    'CliqueId'=>$request->input('CliqueId'),
                    'UseId'=>session('user')[0]->id,
                    'PId'=>$request->input('CliqueId')
                );
                if($request->has('PactId'))
                {
                    $insert['PactId']=$request->input('PactId');
                }
                else
                {
                    $insert['PactId']=0;
                }
                DB::table('business')->where('id',$request->input('listId'))->update($insert);
                return redirect('system/demander/index')->with('callback', '公司修改成功');
                break;
                case 6:
                $insert=array(
                    'Name'=>trim($request->input('Name')),
                    'Type'=>6,
                    'CliqueId'=>$request->input('CliqueId'),
                    'ComId'=>$request->input('ComId'),
                    'UseId'=>session('user')[0]->id,
                    'PId'=>$request->input('ComId')
                );
                if($request->has('PactId'))
                {
                    $insert['PactId']=$request->input('PactId');
                }
                else
                {
                    $insert['PactId']=0;
                }
                DB::table('business')->where('id',$request->input('listId'))->update($insert);
                return redirect('system/demander/index')->with('callback', '部门修改成功');
                break;
                case 8:
                $insert=array(
                    'Name'=>trim($request->input('Name')),
                    'Type'=>8,
                    'CliqueId'=>$request->input('CliqueId'),
                    'ComId'=>$request->input('ComId'),
                    'DepId'=>$request->input('DepId'),
                    'UseId'=>session('user')[0]->id,
                    'PId'=>$request->input('DepId')
                );
                if($request->has('PactId'))
                {
                    $insert['PactId']=$request->input('PactId');
                }
                else
                {
                    $insert['PactId']=0;
                }
                DB::table('business')->where('id',$request->input('listId'))->update($insert);
                return redirect('system/demander/index')->with('callback', '组别修改成功');
                break;
            default:
            return 0;
            }
        }
        else//条目添加功能
        {
            switch($request->input('Type'))
            {
            case 2:
            $insert=array(
                'Name'=>trim($request->input('Name')),
                'Type'=>2,
                'CliqueId'=>0,
                'UseId'=>session('user')[0]->id,
                'PId'=>0
            );
            if($request->input('PactId'))
            {
                $insert['PactId']=$request->input('PactId');
            }
            else
            {
                $insert['PactId']=0;
            }
            DB::table('business')->insert($insert);
            return redirect('system/demander/index')->with('callback', '集团添加成功');
            break;
            case 4:
            $insert=array(
                'Name'=>trim($request->input('Name')),
                'Type'=>4,
                'CliqueId'=>$request->input('CliqueId'),
                'UseId'=>session('user')[0]->id,
                'PId'=>$request->input('CliqueId')
            );
            if($request->input('PactId'))
            {
                $insert['PactId']=$request->input('PactId');
            }
            else
            {
                $insert['PactId']=0;
            }
            DB::table('business')->insert($insert);
            return redirect('system/demander/index')->with('callback', '公司添加成功');
            break;
            case 6:
            $insert=array(
                'Name'=>trim($request->input('Name')),
                'Type'=>6,
                'CliqueId'=>$request->input('CliqueId'),
                'ComId'=>$request->input('ComId'),
                'UseId'=>session('user')[0]->id,
                'PId'=>$request->input('ComId')
            );
            if($request->input('PactId'))
            {
                $insert['PactId']=$request->input('PactId');
            }
            else
            {
                $insert['PactId']=0;
            }
            DB::table('business')->insert($insert);
            return redirect('system/demander/index')->with('callback', '部门添加成功');
            break;
            case 8:
            $insert=array(
                'Name'=>trim($request->input('Name')),
                'Type'=>8,
                'CliqueId'=>$request->input('CliqueId'),
                'ComId'=>$request->input('ComId'),
                'DepId'=>$request->input('DepId'),
                'UseId'=>session('user')[0]->id,
                'PId'=>$request->input('DepId')
            );
            if($request->input('PactId'))
            {
                $insert['PactId']=$request->input('PactId');
            }
            else
            {
                $insert['PactId']=0;
            }
            DB::table('business')->insert($insert);
            return redirect('system/demander/index')->with('callback', '组添加成功');
            break;
            default:
            return 0;
            }
        } 
    }

    //创建树形递归方法
    public function getTree($PId = 0,$typeTree)
    {
        $result=DB::table('business')
        ->LeftJoin('pact','pact.id','=','business.PactId')
        ->select('business.*','pact.Name as PactName')
        ->where('business.PId',$PId)->get();
        foreach ($result as $list_full)
        {
             array_push($typeTree, $list_full);
            $typeTree=self::getTree($list_full->id,$typeTree);
        }
       return $typeTree;
    }

    //使用树形递归方法输出列表
    public function getTree_list(){
        static $typeTree = array();
        $typeTree=$this->getTree($CliqueId= 0,$typeTree);
        return $typeTree;
    }

    //修改条目功能，返回需要修改的条目内容和父级select选项，没有update功能
    public function edit(Request $request)
    {
        $listId=$request->input('listId');
        switch($request->input('Type'))
        {
            case 2:
            $data['data']=DB::table('business')->select('Name','id','PactId')->where('id',$listId)->get();
            return json_encode($data);
            break;
            case 4:
            $data['clique']=DB::table('business')->select('Name','id')->where('Type',2)->where('Del',1)->get();
            $data['data']=DB::table('business')->select('CliqueId','Name','id','PactId')->where('id',$listId)->get();
            return json_encode($data);
            break;
            case 6:
            $data['clique']=DB::table('business')->select('Name','id')->where('Type',2)->where('Del',1)->get();
            $data['Com']=DB::table('business')->select('Name','id')->where('CliqueId',DB::table('business')->where('id',$listId)->pluck('CliqueId'))->where('Type',4)->where('Del',1)->get();
            $data['data']=DB::table('business')->select('CliqueId','ComId','Name','id','PactId')->where('id',$listId)->get();
            return json_encode($data);
            break;
            case 8:
            $data['clique']=DB::table('business')->select('Name','id')->where('Type',2)->where('Del',1)->get();
            $data['Com']=DB::table('business')->select('Name','id')->where('CliqueId',DB::table('business')->where('id',$listId)->pluck('CliqueId'))->where('Type',4)->where('Del',1)->get();
            $data['Dep']=DB::table('business')->select('Name','id')->where('ComId',DB::table('business')->where('id',$listId)->pluck('ComId'))->where('Type',6)->where('Del',1)->get();
            $data['data']=DB::table('business')->select('CliqueId','ComId','DepId','Name','id','PactId')->where('id',$listId)->get();
            return json_encode($data);
            break;
            default:
            return 0;
        }
    }

    public function update(Request $request)
    {
        if($request->has('listId'))
        {
            
        }
        else
        {
            return 0;
        }
    }

    public function destroy(Request $request)
    {
        if($request->input('Del')==1)
        {
            switch($request->input('Type'))
            {
            case 2:
            DB::table('business')->where('id',$request->input('listId'))->update(['Del'=>0]);
            DB::table('business')->where('CliqueId',$request->input('listId'))->update(['Del'=>0]);
            return 1;
            break;
            case 4:
            DB::table('business')->where('id',$request->input('listId'))->update(['Del'=>0]);
            DB::table('business')->where('ComId',$request->input('listId'))->update(['Del'=>0]);
            return 1;
            break;
            case 6:
            DB::table('business')->where('id',$request->input('listId'))->update(['Del'=>0]);
            DB::table('business')->where('DepId',$request->input('listId'))->update(['Del'=>0]);
            return 1;
            break;
            case 8:
            DB::table('business')->where('id',$request->input('listId'))->update(['Del'=>0]);
            return 1;
            break;
            default:
            return 0;
            }
        }
        elseif($request->input('Del')==0)
        {
            switch($request->input('Type'))
            {
            case 2:
            DB::table('business')->where('id',$request->input('listId'))->update(['Del'=>1]);
            return 1;
            break;
            case 4:
            if(DB::table('business')->where('id',DB::table('business')->where('id',$request->input('listId'))->pluck('PId'))->pluck('Del')==1)
            {
                DB::table('business')->where('id',$request->input('listId'))->update(['Del'=>1]);
                return 1;
            }
            else
            {
                return 0;
            }
            break;
            case 6:
            if(DB::table('business')->where('id',DB::table('business')->where('id',$request->input('listId'))->pluck('PId'))->pluck('Del')==1)
            {
                DB::table('business')->where('id',$request->input('listId'))->update(['Del'=>1]);
                return 1;
            }
            else
            {
                return 0;
            }
            break;
            case 8:
            if(DB::table('business')->where('id',DB::table('business')->where('id',$request->input('listId'))->pluck('PId'))->pluck('Del')==1)
            {
                DB::table('business')->where('id',$request->input('listId'))->update(['Del'=>1]);
                return 1;
            }
            else
            {
                return 0;
            }
            break;
            default:
            return 0;
            }
        }
    }

    public function test()
    {

        //调用定义类
        $globals = new globals();

        echo $globals->Jurisdiction(array(24));
        echo $globals->UseSession("name");

        //返回是否有权限
        echo $globals->Jurisdiction(array(2, 4));
        //返回接口格式
        $tt = array(2, 3, 4, 5, 6);
        echo $globals->Return_Url($tt);


    }

}
